//
//  Find_ViewController.m
//  MagicBox
//
//  Created by dllo on 16/8/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Find_ViewController.h"
#import "LoadResigter_ViewController.h"
#import "ChatViewController.h"
#import "UserCenter_ViewController.h"

#import "ContactListViewController.h"  // 好友列表
#import "ConversationListController.h"

#import "BaseTableView.h"
#import "BlockListViewController.h"

@interface Find_ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) BaseTableView *tableView;

@end

@implementation Find_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"中央大厅";
    
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, SPACED, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = SPACED * 6;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [self.tableView reloadData];
}


// 分三区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
// 区头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return SPACED * 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SPACED * 4)];
    imageView.image = [UIImage imageNamed:@"backH.jpg"];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH / 2, SPACED * 4)];
    label.textAlignment = 1;
    label.textColor = [UIColor orangeColor];
    label.center = imageView.center;
    switch (section) {
        case 0:
            label.text = @"喜欢";
            break;
        case 1:
             label.text = @"不喜欢";
            break;
        default:
             label.text = @"设置";
            break;
    }
    
    [imageView addSubview:label];
    
    
    return imageView;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 2;
            break;
        case 1:
            return 1;
            break;
        default:
            return 2;
            break;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    switch (indexPath.section) {
        case 0:
        {
            if (indexPath.row == 0) {
                cell.textLabel.text = @"对话列表";
                cell.imageView.image = [UIImage imageNamed:@"cchat.png"];
            }else{
                cell.textLabel.text = @"好友管理";
                cell.imageView.image = [UIImage imageNamed:@"cfriend.png"];
            }
        }
            break;
        case 1:
        {
            cell.textLabel.text = @"黑名单";
            cell.imageView.image = [UIImage imageNamed:@"cblack.png"];
        }
            break;
            
        default:
        {
            if (indexPath.row == 0) {
                cell.textLabel.text = @"当前登录状态";
                if ([EMClient sharedClient].isLoggedIn) {
                    cell.imageView.image = [UIImage imageNamed:@"cload.png"];
                }else{
                    cell.imageView.image = [UIImage imageNamed:@"cunload.png"];
                }
                
            }else{
                cell.textLabel.text = @"退出当前登录";
                cell.imageView.image = [UIImage imageNamed:@"coff.png"];
            }
        }
            break;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:
        {
            if (indexPath.row == 0) {
                
                [self conversation];
                
            }else{
                
                [self goodFriendsList];
            }
        }
            break;
        case 1:
        {
            [self blockList];
        }
            break;
            
        default:
        {
            if (indexPath.row == 0) {
                
                [self load];
                
            }else{
                [self logout];
            }
        }
            break;
    }

    
    
    
    
}

// 去黑名单看看
-(void)blockList{
    
    BlockListViewController *blockVC = [[BlockListViewController alloc]init];

    [self.navigationController pushViewController:blockVC animated:YES];
}


// 退出当前登录
-(void)logout{
   
     UIAlertController *logoutAC = [UIAlertController alertControllerWithTitle:@"是否确认退出" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
         EMError *error = [[EMClient sharedClient] logout:YES];
        if (!error) {
            NSLog(@"退出成功");
            [self.tableView reloadData];
        }

    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
       
  
    [logoutAC addAction:confirmAction];
    [logoutAC addAction:cancelAction];
    
    [self presentViewController:logoutAC animated:YES completion:nil];
    
    
}



// 会话列表
-(void)conversation{
    
    ConversationListController *conVC = [[ConversationListController alloc]init];
    [self.navigationController pushViewController:conVC animated:YES];
    
}


// 好友列表
-(void)goodFriendsList{
  
    ContactListViewController *contackList = [[ContactListViewController alloc]init];
    [self.navigationController pushViewController:contackList animated:YES];
    
}


-(void)load{
    
    UserCenter_ViewController *userVC = [[UserCenter_ViewController alloc]init];
    
    [self presentViewController:userVC animated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)didReceiveFriendInvitationFromUsername:(NSString *)aUsername
                                       message:(NSString *)aMessage{
    
    NSLog(@"我收到了请求, userName:%@, aMessage: %@", aUsername, aMessage);
     
}













@end
